package game.games.paint;

import game.Logger;
import game.Position;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;


public class CellMap implements IResizeable
{
    protected static boolean KEYLISTENER_ENABLED = false;

    private short cellSize = 16;
    
    private Position size;
    
    public CellMap(boolean keyListenerEnabled)
    {
        KEYLISTENER_ENABLED = keyListenerEnabled;
    }
    
    protected void setCellSize(int cellSize)
    {
        this.cellSize = (short) cellSize;
    }
    
    protected short getCellSize()
    {
        return cellSize;
    }
    
    public Position getSize()
    {
        return size;
    }
    
    public void setSize(Position size)
    {
        this.size = size;
    }
    
    @Override
    public void recalcSize(Dimension windowSize)
    {
        Position maxXY = calcMaxSize(new Position(windowSize));
        setSize(maxXY);
    }
    
    public int calculateIndex(Position position)
    {
        return calculateIndex(position.getX(), position.getY(), getSize());
    }
    
    public int calculateIndex(Position position, Position size)
    {
        return calculateIndex(position.getX(), position.getY(), size);
    }
    
    public int calculateIndex(int x, int y)
    {
        return calculateIndex(x, y, getSize());
    }
    
    public static int calculateIndex(int x, int y, Position size)
    {
        if (x < 0 || x > size.getWidth() || y < 0 || y > size.getHeight())
        {
            return 0;
        }
        int index = (size.getWidth() * y) + x;
        return index;
    }
    
    public Position calculatePosition(int index)
    {
        return calculatePosition(index, getSize());
    }
    
    public static Position calculatePosition(int index, Position size)
    {
        int x = 0;
        int y = 0;
        // find y
        y = index / size.getX();
        x = index - (y * size.getX());
        return new Position(x, y);
    }
    
    public Position calcMaxSize(Position windowSize)
    {
        int width = windowSize.getWidth();
        if (width % getCellSize() != 0)
        {
            width -= width % getCellSize();
        }
        width /= getCellSize();
        int height = windowSize.getHeight();
        if (height % getCellSize() != 0)
        {
            height -= height % getCellSize();
        }
        height /= getCellSize();
        return new Position(width, height);
    }
    
    public Position calcCoord(java.awt.Point p)
    {
        int x = (int) (p.getX() / getCellSize());
        int y = (int) (p.getY() / getCellSize());
        return new Position(x, y);
    }
    
    public void fillRect(Graphics g, Position position, Color color)
    {
        fillRect(g, position.getX(), position.getY(), color);
    }
    
    public void fillRect(Graphics g, int x, int y, Color color)
    {
        fillRect(g, x * getCellSize(), y * getCellSize(), getCellSize(), getCellSize(), color);
    }
    
    public void fillRect(Graphics g, int x, int y, int width, int height, Color color)
    {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }
    
    public void drawRect(Graphics g, Position position, Color color)
    {
        drawRect(g, position.getX(), position.getY(), color);
    }
    
    public void drawRect(Graphics g, int x, int y, Color color)
    {
        drawRect(g, x * getCellSize(), y * getCellSize(), getCellSize(), getCellSize(), color);
    }
    
    public void drawRect(Graphics g, int x, int y, int width, int height, Color color)
    {
        g.setColor(color);
        g.drawRect(x, y, width, height);
    }
    
    public void drawImage(Graphics g, int x, int y, BufferedImage img)
    {
        g.drawImage(img, x * img.getWidth(), y * img.getHeight(), null);
    }
    
    public KeyListener getKeyAdapter()
    {
        return new KeyAdapter() 
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if (KEYLISTENER_ENABLED)
                {
                    short newCellSize = getCellSize();
                    switch (e.getKeyCode())
                    {
                        case KeyEvent.VK_1:
                        {
                            newCellSize = 1;
                            break;
                        }
                        case KeyEvent.VK_2:
                        {
                            newCellSize = 2;
                            break;
                        }
                        case KeyEvent.VK_3:
                        {
                            newCellSize = 3;
                            break;
                        }
                        case KeyEvent.VK_4:
                        {
                            newCellSize = 4;
                            break;
                        }
                        case KeyEvent.VK_5:
                        {
                            newCellSize = 5;
                            break;
                        }
                        case KeyEvent.VK_6:
                        {
                            newCellSize = 6;
                            break;
                        }
                        case KeyEvent.VK_7:
                        {
                            newCellSize = 7;
                            break;
                        }
                        case KeyEvent.VK_8:
                        {
                            newCellSize = 8;
                            break;
                        }
                        case KeyEvent.VK_9:
                        {
                            newCellSize = 9;
                            break;
                        }
                        case KeyEvent.VK_0:
                        {
                            newCellSize = 10;
                            break;
                        }
                    }
                    if (newCellSize > 0 && newCellSize != getCellSize())
                    {
                        Logger.log("cellSize: " + newCellSize);
                        setCellSize(newCellSize);
                    }
                }
            }
        };
    }
}
